<?php
namespace app\admin\model\order;
use app\common\enum\order\PayStatus as PayStatusEnum;
use app\common\enum\order\PayType as PayTypeEnum;
use app\common\enum\order\WriteStatus as WriteStatusEnum;
use app\common\library\helper;
use app\common\service\Order as OrderService;
use think\Model;

class GiftsOrder extends Model
{
    protected $name = 'gifts_order';
    protected $alias = 'order';
    /**
     * 订单商品列表
     * @return \think\model\relation\HasMany
     */
    public function goods()
    {
        return $this->hasMany("GiftsOrderGoods",'order_id','order_id');
    }

    public function wxapp(){
        return $this->belongsTo("app\admin\model\wxapp\Wxapp",'wxapp_id','wxapp_id');
    }

    /**
     * 关联用户表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo("app\admin\model\user\User");
    }


    /**
     * 付款状态
     * @param $value
     * @return array
     */
    public function getPayTypeAttr($value)
    {
        return ['text' => PayTypeEnum::data()[$value]['name'], 'value' => $value];
    }

    /**
     * 付款状态
     * @param $value
     * @return array
     */
    public function getPayStatusAttr($value)
    {
        return ['text' => PayStatusEnum::data()[$value]['name'], 'value' => $value];
    }


    /**
     * 核销状态
     * @param $value
     * @return array
     */
    public function getWriteStatusAttr($value)
    {
        return ['text' => WriteStatusEnum::data()[$value]['name'], 'value' => $value];
    }

    /**
     * 付款状态
     * @param $value
     * @return array
     */
    public function getWriteTimeAttr($value)
    {
        return ['text' => $value?date('Y-m-d H:i:s',$value):"---", 'value' => $value];
    }


    /**
     * 生成订单号
     * @return string
     */
    public function orderNo()
    {
        return OrderService::createOrderNo();
    }

    /**
     * 批量获取订单列表
     * @param $orderIds
     * @param array $with 关联查询
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByIds($orderIds, $with = [])
    {

        $data = $this->getListByInArray('order_id', $orderIds, $with);
        return helper::arrayColumn2Key($data, 'order_id');
    }


    /**
     * 批量获取订单列表
     * @param string $field
     * @param array $data
     * @param array $with
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getListByInArray($field, $data, $with = [])
    {

        return $this->with($with)
            ->where($field, 'in', $data)
            ->where('is_delete', '=', 0)
            ->select();
    }



    /**
     * 订单详情
     * @param array|int $where
     * @param array $with
     * @return null|\app\common\model\GiftsOrder
     * @throws \think\exception\DbException
     */
    public static function detail($where, $with = [
        'user',
        'address',
        'goods' => ['image'],
        'extract',
        'express',
        'extract_shop.logo',
        'extract_clerk'
    ])
    {
        is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;

        return self::get($filter, $with);
    }



    /**
     * 订单列表
     * @param string $dataType
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($query = [])
    {

        // 检索查询条件
        !empty($query) && $this->setWhere($query);
        // 获取数据列表
        return $this->with(['goods.image', 'user','wxapp'])
            ->alias('order')
            ->field('order.*')
            ->join('wxapp', 'wxapp.wxapp_id = order.wxapp_id')
            ->join('user', 'user.user_id = order.user_id')
            ->where('order.is_delete', '=', 0)
            ->order(['order.create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);

    }

    /**
     * 订单列表(全部)
     * @param $dataType
     * @param array $query
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListAll($query = [])
    {
        // 检索查询条件
        !empty($query) && $this->setWhere($query);
        // 获取数据列表
        return $this->with(['goods.image', 'user'])
            ->alias('order')
            ->field('order.*')
            ->join('user', 'user.user_id = order.user_id')
            ->where('order.is_delete', '=', 0)
            ->order(['order.create_time' => 'desc'])
            ->select();
    }





    /**
     * 设置检索查询条件
     * @param $query
     */
    private function setWhere($query)
    {

        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('order.order_no|user.nickName|wxapp.wxapp_name', 'like', '%' . trim($query['search']) . '%');
        }

//        if (isset($query['start_time']) && !empty($query['start_time'])) {
//            $this->where('order.create_time', '>=', strtotime($query['start_time']));
//        }
//        if (isset($query['end_time']) && !empty($query['end_time'])) {
//            $this->where('order.create_time', '<', strtotime($query['end_time']) + 86400);
//        }
        if (isset($query['search_time']) && !empty($query['search_time'])) {
            $times=explode('至',$query['search_time']);
            $this->where('order.create_time','between time',$times);
        }
        if (isset($query['pay_status']) && !empty($query['pay_status'])) {
            $query['pay_status'] > -1 && $this->where('pay_status', '=', $query['pay_status']);
        }
        if (isset($query['write_status']) && !empty($query['write_status'])) {
            $query['write_status'] > -1 && $this->where('write_status', '=', $query['write_status']);
        }

    }
}